package org.wanda.pv.controller.hy;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.annotation.Comment;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.model.basic.Point;
import org.wanda.pv.model.basic.PointClass;
import org.wanda.pv.framework.constant.AttachmentType;
import org.wanda.pv.model.business.Attachment;
import org.wanda.pv.model.basic.Specialty;
import org.wanda.pv.model.business.Mark;
import org.wanda.pv.model.business.MarkFileRe;
import org.wanda.pv.model.business.ProjectCheckState;
import org.wanda.pv.model.hy.HyDrawing;
import org.wanda.pv.model.hy.HyTree;
import org.wanda.pv.model.hy.HyUserInfo;
import org.wanda.pv.service.hy.HyProjectInfoService;
import org.wanda.pv.util.HyUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * @author lxy
 * Created by 2018/12/25 14:58
 */

@RestController
@RequestMapping("/hy/projectInfo")
public class HyProjectInfoController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	Dao dao;
	@Autowired
	private HyProjectInfoService hyProjectInfoService;





	@RequestMapping("/getAttachment")
	public Object getAttachment(HttpServletRequest request){

		try{
			String projectId = (String) request.getSession().getAttribute("projectId");
			List<Attachment> attachments = dao.query(Attachment.class, Cnd.where("projectInfoId", "=", projectId).and("type", "=", AttachmentType.REVIEW));
			return  Result.success("success",attachments);

		}catch (Exception e){
			logger.error(e.getMessage(),e);
			return Result.error("fail","服务器内部错误!");

		}

	}



	/**
	 * 获取专业下图纸的统计信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/summaryOfSpecialty")
	public Object summaryOfSpecialty(HttpServletRequest request){
		String projectId = (String) request.getSession().getAttribute("projectId");
		if (projectId ==null ){
			return Result.error("用户信息获取失败，请重新登录","用户信息获取失败，请重新登录");
		}
		List<HyTree> hyTrees = dao.query(HyTree.class, Cnd.where("parentId", "=", "116").and("delFlag","=","0"));
		int allCount = 0;
		ArrayList<Object> objects = new ArrayList<>();
		for (HyTree hyTree :hyTrees){
			int markNum = 0 ;
			List<HyDrawing> query = dao.query(HyDrawing.class, Cnd.where("flow_map_model_id", "=", hyTree.getId()).and("hy_project_info_id", "=", projectId).and("latest","=",true));
			for (HyDrawing hyDrawing: query) {
				int i=	dao.count(MarkFileRe.class,Cnd.where("hy_drawing_id","=",hyDrawing.getId()));
				allCount = i + allCount;
				markNum = markNum + i;
			}
			HashMap<Object, Object> map = new HashMap<>();
			map.put("count",markNum);
			map.put("flowMapModelName",hyTree.getName());
			objects.add(map);
		}

		HashMap<Object, Object> hashMap = new HashMap<>();
		hashMap.put("allCount",allCount);
		hashMap.put("summaryOfSpecialty",objects);
		return Result.success("success",hashMap);
	}

	/**
	 * 获取问题分类的统计信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/summaryOfPointClass")
	public Object summaryOfPoint(HttpServletRequest request){
		String projectId = (String) request.getSession().getAttribute("projectId");
		if (projectId ==null ){
			return Result.error("用户信息获取失败，请重新登录","用户信息获取失败，请重新登录");
		}
		int class_A = 0;
		int class_B = 0;
		int class_C = 0;
		int class_D = 0;
		int class_E = 0;
		int allCount = 0;
		List<HyDrawing> query = dao.query(HyDrawing.class, Cnd.where("hy_project_info_id", "=", projectId).and("latest","=",true));
		for (HyDrawing hyDrawing: query) {
			List<MarkFileRe> markFileReList = dao.query(MarkFileRe.class, Cnd.where("hy_drawing_id", "=", hyDrawing.getId()));
			allCount = markFileReList.size() + allCount;
			for (MarkFileRe markFileRe: markFileReList) {
				Mark mark = dao.fetch(Mark.class,markFileRe.getMarkId());
				Point point = dao.fetch(Point.class, mark.getPointId());
				dao.fetchLinks(point,"pointClass");
				switch (point.getPointClass().getName()){
					case "A" :
						class_A = ++class_A;
						break;
					case "B" :
						class_B = ++class_B;
						break;
					case "C" :
						class_C = ++class_C;
						break;
					case "D" :
						class_D = ++class_D;
						break;
					case "E" :
						class_E = ++class_E;
						break;
						default:
							break;
				}
			}
		}
		HashMap<Object, Object> hashMapA = new HashMap<>();
		hashMapA.put("count",class_A);
		hashMapA.put("pointClassName","A");
		HashMap<Object, Object> hashMapB = new HashMap<>();
		hashMapB.put("count",class_B);
		hashMapB.put("pointClassName","B");
		HashMap<Object, Object> hashMapC = new HashMap<>();
		hashMapC.put("count",class_C);
		hashMapC.put("pointClassName","C");
		HashMap<Object, Object> hashMapD = new HashMap<>();
		hashMapD.put("count",class_D);
		hashMapD.put("pointClassName","D");
		HashMap<Object, Object> hashMapE = new HashMap<>();
		hashMapE.put("count",class_E);
		hashMapE.put("pointClassName","E");
		List<Map> list = new ArrayList<>();
		list.add(hashMapA);
		list.add(hashMapB);
		list.add(hashMapC);
		list.add(hashMapD);
		list.add(hashMapE);
		HashMap<Object, Object> hashMap = new HashMap<>();
		hashMap.put("allCount",allCount);
		hashMap.put("summaryOfPointClass",list);
		return Result.success("success",hashMap);
	}


	@RequestMapping("/checkProject")
	public Object checkProject(HttpServletRequest request){
		String projectId = (String) request.getSession().getAttribute("projectId");
		HyUserInfo hyUserInfo = (HyUserInfo) request.getSession().getAttribute("userInfo");
		String access_token = (String) request.getSession().getAttribute("access_token");
		String projectFlowChildrenNodeId = (String) request.getSession().getAttribute("projectFlowChildrenNodeId");
		if (projectId ==null || hyUserInfo ==null){
			return Result.error("用户信息获取失败，请重新登录","用户信息获取失败，请重新登录");
		}
		Result result = (Result) hyProjectInfoService.checkProject(projectId,hyUserInfo.getId(),access_token , projectFlowChildrenNodeId);
		return result;
	}

	@RequestMapping("/getProjectCheckState")
	public Object getProjectCheckState(HttpServletRequest request){
		String projectId = (String) request.getSession().getAttribute("projectId");
		if (projectId == null){
			return Result.error("用户信息获取失败，请重新登录","用户信息获取失败，请重新登录");
		}
		ProjectCheckState projectCheckState = dao.fetch(ProjectCheckState.class, Cnd.where("hy_project_id","=",projectId));
		if (projectCheckState == null ){
			projectCheckState = new ProjectCheckState();
			projectCheckState.setProjectCheckSchedule(-1);
			projectCheckState.setProjectCheckState(-1);
		}
		return Result.success("success",projectCheckState);
	}


}
